<template>
  <div>
    <!-- 树形数据 -->
    <el-tree
      ref="tree"
      node-key="id"
      :data="treeList"
      show-checkbox
      default-expand-all
      check-strictly=""
      :props="{label:'name'}"
    />
    <div style="margin-top: 20px;text-align:right" />
    <el-button type="primary" @click="hAssignPer">确定</el-button>
    <el-button @click="hCancel">取消</el-button>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { tranDataTree } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      treeList: []
    }
  },

  created() {
    this.loadPermissionList()
  },

  methods: {
    // 点击确定保存角色分配
    async hAssignPer() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        console.log('当前选中的节点数组是', permIds)
        await assignPerm({ id: this.id, permIds: permIds })
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList---res', res)
        // 保存数据
        this.treeList = tranDataTree(res.data)
        // 获取当前角色的权限点
        const info = await getRoleDetail(this.id)
        // 拥有的权限  处于选中状态
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        console.log('getRoleDetail----info', info)
      } catch (error) {
        console.log(error)
      }
    },
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>
